package gowcage.testandroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gowcage.testandroid.R;

import java.util.List;
import java.util.Random;

import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/11.
 */

public class CheckEmailDemo extends Activity {

    @BindViews({R.id.checkmail_edit_email, R.id.checkmail_edit_email_checkCode})
    List<EditText> editList;
    @BindViews({R.id.checkmail_btn_send, R.id.checkmail_btn_Check})
    List<Button> btnList;

    String code = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkemail);
        ButterKnife.bind(this);
    }

    private String getCheckCode() {
        String code = "";
        for (int i = 0; i < 6; i++) {
            code += new Random().nextInt(10) + "";
        }
        this.code = code;
        Log.i("CheckEmail-->", "getCheckCode: " + code);
        return code;
    }


    //发送Email
    private void sendEmail() {
        final String email = editList.get(0).getText().toString().trim();

        /*Uri uri = Uri.parse("mailto:" + email);
        Intent i1 = new Intent(Intent.ACTION_SENDTO, uri);
        i1.putExtra(Intent.EXTRA_TEXT, "验证码：" + getCheckCode());
        startActivity(i1);*/

        /*Intent i2 = new Intent(Intent.ACTION_SEND);
        i2.putExtra(Intent.EXTRA_EMAIL, email);//接收邮箱
        i2.putExtra(Intent.EXTRA_TEXT, "验证码："+getCheckCode());//邮件内容
        // i2.setType("text/plain"); //模拟器请使用这行
        i2.setType("message/rfc822"); // 真机上使用这行
        startActivity(Intent.createChooser(i2, "Choose Email Client"));*/

        Intent i3 = new Intent(Intent.ACTION_SEND);
        String[] tos = {email};
        String[] ccs = {"you@abc.com"};
        i3.putExtra(Intent.EXTRA_EMAIL, tos);
//        i3.putExtra(Intent.EXTRA_CC, ccs);
        i3.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");//邮件主题
        i3.putExtra(Intent.EXTRA_TEXT, "验证码："+getCheckCode());
        // i3.setType("text/plain"); //模拟器请使用这行
        i3.setType("message/rfc822"); // 真机上使用这行
        startActivity(Intent.createChooser(i3, "Choose Email Client"));
    }

    //验证校验码
    private boolean checkCode(String code) {
        if (code != "") {
            if (code.equals(this.code)) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /*
    监听
     */
    @OnClick({R.id.checkmail_btn_send, R.id.checkmail_btn_Check})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.checkmail_btn_send:
                sendEmail();
                break;
            case R.id.checkmail_btn_Check:
                boolean result = checkCode(editList.get(1).getText().toString().trim());
                if (result) {
                    Toast.makeText(this, "验证成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "验证失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
